<template>
	<view class="bg">
		<view class="container">
			<view class="logo">申请司机</view>
			<input class="input" type="text" placeholder="请输入车牌号" v-model="plate" />
			<input class="input" type="text" placeholder="请输入真实姓名" v-model="realName" />
			<button class="btn" @click="registerHandle">提交申请</button>
			<view class="links">
				<text class="link" @click="toLogin">返回</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { register, getUserInfo } from '@/utils/api.js';
import useStorage from '../../utils/useStorage';

const plate = ref('陕A88888');
const realName = ref('李四');

// 注册
async function registerHandle() {
	try {
		if (!plate.value || !realName.value) {
			uni.showToast({ title: '请填写完整信息', icon: 'none' });
			return;
		}
		const res = await register({
			driver_license: plate.value,
			real_name: realName.value
		});
		uni.showToast({ title: '注册成功', icon: 'success' });

		const userInfo = await getUserInfo();
		useStorage.set('userInfo', userInfo.account);

		setTimeout(() => {
			uni.reLaunch({
				url: '/pages/index/index'
			});
		}, 1000);
	} catch (error) {
		console.log(error);
	}
}

function toLogin() {
	uni.navigateBack();
	// 或 uni.reLaunch({ url: '/pages/login/login' })
}
</script>

<style scoped lang="scss">
@import '@/common/css/login.scss';
</style>
